package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.MemberMapper;
import com.itheima.pojo.Member;
import com.itheima.pojo.PageBean;
import com.itheima.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;
import java.util.List;

@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public List<Member> getList() {
        List<Member> list = memberMapper.getList();
        return list;
    }

    @Override
    public Member getById(int id) {
        Member member = memberMapper.getById(id);
        return member;
    }

    @Override
    public PageBean getPage(int page, int pageSize) {
        // 1、设置分页参数
        PageHelper.startPage(page, pageSize);
        // 2、执行查询
        List<Member> memberList = memberMapper.getPage();
        Page<Member> pageMember = (Page<Member>) memberList;

        PageBean pageBean = new PageBean(pageMember.getTotal(),pageMember.getResult());
        return pageBean;
    }

    @Override
    public void update(Member member) {
        member.setUpdateTime(LocalDateTime.now());
        memberMapper.update(member);
    }

    @Override
    public void delete(int id) {
        memberMapper.delete(id);
    }

    @Override
    public void add(Member member) {
        member.setCreateTime(LocalDateTime.now());
        member.setUpdateTime(LocalDateTime.now());
        memberMapper.add(member);
    }
}
